package org.rc.six;

import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * IndexDao that uses memory to store the Index 
 * @author dokeeffe
 */
public class InMemoryIndexDao implements IndexDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(InMemoryIndexDao.class);
    private static final int DEFAULT_IDX_STEM = 2;
    private Map<String, Index> dataStore = new HashMap<String, Index>();

    @Override
    public Index getIndex(String term) {
        String stem = term.substring(0, DEFAULT_IDX_STEM);
        LOGGER.debug("getting index for term: "+term + " stem: "+stem);
        Index idx = dataStore.get(stem);
        if (idx == null) {
            idx = new Index(stem);
            dataStore.put(stem, idx);
        }
        return idx;
    }

    @Override
    public void storeIndex(Index ix) {
        //nothing to do for this one
    }

}
